package controller;

import model.Grade;
import model.Student;
import service.GradeService;
import service.StudentService;
import service.impl.GradeServiceImpl;
import service.impl.StudentServiceImpl;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("addStudent".equals(method)) {
            addStudent(req, resp);
        } else if ("deleteStats".equals(method)) {
            deleteStats(req, resp);
        } else if ("deleteStudentById".equals(method)) {
            deleteStudentById(req, resp);
        } else if ("findById".equals(method)) {
            findById(req, resp);
        } else if ("studentList".equals(method)) {
            studentList(req, resp);
        } else if ("updateStudent".equals(method)) {
            updateStudent(req, resp);
        } else {
            studentList(req, resp);
        }

    }

    protected void addStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Student student = new Student();
        student.setStuName(req.getParameter("stuName"));
        student.setStuNo(req.getParameter("stuNo"));
        student.setStuSex(Integer.parseInt(req.getParameter("stuSex")));
        student.setStuPhone(req.getParameter("stuPhone"));
        student.setStuEmail(req.getParameter("stuEmail"));
        student.setStuRegistered(req.getParameter("stuRegistered"));
        student.setStuAddress(req.getParameter("stuAddress"));
        student.setStuProfession(req.getParameter("stuProfession"));
        student.setStuIdnumber(req.getParameter("stuIdnumber"));
        student.setStuPolitice(req.getParameter("stuPolitice"));
        student.setStuIntroduction(req.getParameter("stuIntroduction"));
        student.setGid(Integer.parseInt(req.getParameter("gid")));
        int i = studentService.addStudent(student);
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            //添加成功
            writer.println("<script>alert('添加成功');location.href='studentServlet?method=studentList'</script>");
        } else {
            //添加失败
            writer.println("<script>alert('添加失败');location.href='getgrade'</script>");
        }
    }

    protected void deleteStudentById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String sid = req.getParameter("sid");
        int i = studentService.deleteById(Integer.parseInt(sid));
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            //添加成功
            writer.println("<script>alert('删除成功');location.href='studentServlet?method=studentList'</script>");
        } else {
            //添加失败
            writer.println("<script>alert('删除失败');location.href='studentServlet?method=studentList'</script>");
        }
    }

    protected void deleteStats(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String sid = req.getParameter("sid");

        System.out.println(sid);

        int i = studentService.deleteStats(Integer.parseInt(sid));
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            //添加成功
            writer.println("<script>alert('退学成功');location.href='studentServlet?method=studentList'</script>");
        } else {
            //添加失败
            writer.println("<script>alert('退学失败');location.href='studentServlet?method=studentList'</script>");
        }

    }

    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Student student = new Student();
        student.setStuId(Integer.parseInt(req.getParameter("stuId")));
        student.setStuName(req.getParameter("stuName"));
        student.setStuSex(Integer.parseInt(req.getParameter("stuSex")));
        student.setStuPhone(req.getParameter("stuPhone"));
        student.setStuEmail(req.getParameter("stuEmail"));
        student.setStuRegistered(req.getParameter("stuRegistered"));
        student.setStuAddress(req.getParameter("stuAddress"));
        student.setStuProfession(req.getParameter("stuProfession"));
        student.setStuPolitice(req.getParameter("stuPolitice"));
        student.setStuIntroduction(req.getParameter("stuIntroduction"));
        student.setGid(Integer.parseInt(req.getParameter("gid")));
        int i = studentService.updateStudent(student);
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            //添加成功
            writer.println("<script>alert('更新成功');location.href='studentServlet?method=studentList'</script>");
        } else {
            //添加失败
            writer.println("<script>alert('更新失败');location.href='studentServlet?method=findById&sid=" + student.getStuId() + "'</script>");
        }
    }

    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        Student student = studentService.findById(Integer.parseInt(sid));

        //查询年级列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> grades = gradeService.getGrades();

        req.setAttribute("studentGrade", grades);
        req.setAttribute("student", student);
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }

    protected void studentList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //模糊查
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String stusex = req.getParameter("stusex");
        int sex = stusex == null || stusex.equals("") ? -1 : Integer.parseInt(stusex);

        PageUtil pageUtil = new PageUtil();

        //分页 limit 开始位置，显示条数
        String pageIndex = req.getParameter("pageIndex");
        //页面没有传入pageIndex的时候，默认查询首页
        int index = pageIndex == null ? 1 : Integer.parseInt(pageIndex);

        List<Student> all = studentService.getAll(stuname, stuno, sex, index, pageUtil.getPageSize());

        //获取总页数=总条数%每一页显示的条数>0---->总条数/每一页显示条数   然后+1操作
        //获取总页数=总条数%每一页显示的条数=0---->总条数/每一页显示条数
        int total = studentService.total(stuname, stuno, sex);//总条数
        pageUtil.setTotal(total);
        pageUtil.setDateList(all);
        pageUtil.setPageIndex(index);

        req.setAttribute("studentList", all);
        req.setAttribute("stuname", stuname);
        req.setAttribute("stuno", stuno);
        req.setAttribute("stusex", sex);

        req.setAttribute("p1", pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }
}
